package com.kgc.sbt.provider;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kgc.sbt.api.VipUserApi;
import com.kgc.sbt.domain.Sign;
import com.kgc.sbt.domain.SignExample;
import com.kgc.sbt.domain.User;
import com.kgc.sbt.domain.Vip;
import com.kgc.sbt.mapper.SignMapper;
import com.kgc.sbt.mapper.UserMapper;
import com.kgc.sbt.mapper.VipMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Andy
 * @time: 2021/6/23 19:39
 */
@Service
@Slf4j
public class VipUserProvider implements VipUserApi {
    @Autowired
    private VipMapper vipMapper;
    @Autowired
    private SignMapper signMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean addScore(Integer userId, Integer score) {
        Vip vip = vipMapper.selectVipByUserId(userId);
        vip.setScore(vip.getScore()+score);
        if(vipMapper.updateByPrimaryKeySelective(vip)>0){
            return true;
        }else{
            return false;
        }
    }

    @Override
    public Integer queryScore(Integer userId) {
        return vipMapper.selectScoreByUserId(userId);
    }

    @Override
    public Boolean reduceScore(Integer userId, Integer score) {
        Vip vip = vipMapper.selectVipByUserId(userId);
        if(vip.getScore()-score>=0){
            vip.setScore(vip.getScore() - score);
            if(vipMapper.updateByPrimaryKeySelective(vip)>0){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @Override
    public Integer userSigned(Integer userId) {
        String msg;
        Boolean falg=false;
        Date date= new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat df2 = new SimpleDateFormat("dd");

        log.error("进入userSigned方法2++++++++");
        List<Integer> list = signMapper.selectAllUserId();
        for(Integer i:list){
            if(i.equals(userId)){
                falg=true;
            }
        }
        if(falg){

//           Sign sign = signMapper.selectByUserId(userId);
            SignExample signExample = new SignExample();
            signExample.setDistinct(false);
            signExample.setOrderByClause("id desc");
            SignExample.Criteria criteria = signExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            List<Sign> signList =  signMapper.selectByExample(signExample);
            Sign sign = new Sign();
            for(Sign sign2 : signList){
                sign = sign2;
            }
            Date date2 = sign.getUpdateTime();
            String day1 = df2.format(date);
            String day2 =  df2.format(date2);
            long time = System.currentTimeMillis();
            long yestodaytime = time - (1000 * 60 * 60 * 24);
            String s =  df2.format(yestodaytime);
            String s2 = df2.format(time);
            if(s.equals(day2)){
                Integer conSign = sign.getContinueSign()+1;
                sign.setContinueSign(conSign);
                sign.setCount(sign.getCount()+1);
            }else if(s2.equals(day1)){
//               当天多次签到的情况
                msg = "今天已经签到完成，明天再来吧！";
                return 2;
            }else {
                sign.setContinueSign(0);
                sign.setCount(sign.getCount()+1);
            }
            if(sign.getContinueSign()==7){
                addScore(userId,20);
                sign.setContinueSign(0);
            }
            sign.setUserId(userId);
            sign.setUpdateTime(date);
            if(signMapper.updateByPrimaryKey(sign)>0){
                return 1;
            }else {
                return 0;
            }
        }else{
            Sign sign = new Sign();
            sign.setUserId(userId);
            sign.setCount(1);
            sign.setContinueSign(1);
            sign.setCreatTime(date);
            sign.setUpdateTime(date);
            if(signMapper.insert(sign)>0){
                return 1;
            }else {
                return 3;
            }
        }
    }

    @Override
    public Sign querySignByUserId(Integer userId) {
        return signMapper.selectByUserId(userId);
    }

    @Override
    public Boolean queryUserById(Integer id) {
       User user= userMapper.selectByPrimaryKey(id);
       if(user!=null){
           return true;
       }
        return false;
    }


    public String queryWlInfo(Integer lId, String lnumber) {
        return null;
    }


}
